package cn.restcloud.example.controller.hbase;

import cn.restcloud.framework.core.annotation.bean.BeanConfig;
import cn.restcloud.framework.core.annotation.bean.BeanType;
import cn.restcloud.framework.core.annotation.rest.MethodType;
import cn.restcloud.framework.core.annotation.rest.Param;
import cn.restcloud.framework.core.annotation.rest.Params;
import cn.restcloud.framework.core.annotation.rest.RestConfig;
import cn.restcloud.framework.core.util.db.hbase.HBaseService;
import cn.restcloud.framework.view.base.DocAndView;
import cn.restcloud.framework.view.util.ViewUtil;

import java.util.Arrays;

@BeanConfig(appId = "Hello",beanId = "TestHbaseSql",beanName = "HBase链接测试",beanType = BeanType.CONTROLLER)
@RestConfig(url = "/rest/hbase")
public class TestHbaseSql {

	@RestConfig(configName = "HBase测试",method = MethodType.POST,url = "/hbase")
	@Params({
		@Param(id = "password",name = "密码",required=false),
		@Param(id = "card",name = "用户编号",required=false)
	})
	public DocAndView hbaseTest(String password,String card) throws Exception{
		HBaseService hBaseService=HBaseService.getHbaseService("HBase");
		
		return ViewUtil.getJsonView(hBaseService.getConnection());
		
	}

	 public static void main(String[] args) throws Exception {
		 TestHbaseSql testHbaseSql=new TestHbaseSql();
		 testHbaseSql.testCreateTable();
	 }
	 
    public void testCreateTable() throws Exception {
    	HBaseService hbaseService=HBaseService.getHbaseService("hbase");
    	
        //删除表
        hbaseService.deleteTable("test_base");

        //创建表
        hbaseService.createTableBySplitKeys("test_base", Arrays.asList("f","back"),hbaseService.getSplitKeys(null));

        //插入三条数据
        hbaseService.putData("test_base","66804_000001","f",new String[]{"project_id","varName","coefs","pvalues","tvalues","create_time"},new String[]{"40866","mob_3","0.9416","0.0000","12.2293","null"});
        hbaseService.putData("test_base","66804_000002","f",new String[]{"project_id","varName","coefs","pvalues","tvalues","create_time"},new String[]{"40866","idno_prov","0.9317","0.0000","9.8679","null"});
        hbaseService.putData("test_base","66804_000003","f",new String[]{"project_id","varName","coefs","pvalues","tvalues","create_time"},new String[]{"40866","education","0.8984","0.0000","25.5649","null"});

/*        //查询数据
        //1. 根据rowKey查询
        Map<String,String> result1 = hbaseService.getRowData("test_base","66804_000001");
        System.out.println("+++++++++++根据rowKey查询+++++++++++");
        result1.forEach((k,value) -> {
            System.out.println(k + "---" + value);
        });
        System.out.println();

        //精确查询某个单元格的数据
        String str1 = hbaseService.getColumnValue("test_base","66804_000002","f","varName");
        System.out.println("+++++++++++精确查询某个单元格的数据+++++++++++");
        System.out.println(str1);
        System.out.println();

        //2. 遍历查询
        Map<String,Map<String,String>> result2 = hbaseService.getResultScanner("test_base");
        System.out.println("+++++++++++遍历查询+++++++++++");
        result2.forEach((k,value) -> {
            System.out.println(k + "---" + value);
        });*/
    }

}